package com.qiaowan.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJframe extends JFrame implements KeyListener, ActionListener {

    String path = "image\\animal\\animal3\\";
    int[][] win = {
            {1, 5, 9, 13},
            {2, 6, 10, 14},
            {3, 7, 11, 15},
            {4, 8, 12, 0}
    };
    int[][] date = new int[4][4];
    //记录空白在二维数组的位置
    int x;
    int y;
    //记录步数初始为0
    int step = 0;
    //创建选项下面的条目对象
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reloginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem accountItem = new JMenuItem("联系我");
    JMenuItem animals = new JMenuItem("动物");
    JMenuItem girls = new JMenuItem("美女");
    JMenuItem sports = new JMenuItem("运动");

    //游戏界面
    public GameJframe() {
        //初始化界面
        initJFrame();
        //初始化菜单
        initJMenuBar();
        //初始化数据(打乱)
        initData();
        //初始化图片
        initImage();
        //让界面显示出来
        this.setVisible(true);
    }

    //打乱图片代码
    private void initData() {
        int[] temArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < temArr.length; i++) {
            int index = r.nextInt(temArr.length);
            int temp = temArr[i];
            temArr[i] = temArr[index];
            temArr[index] = temp;
        }
        for (int i = 0; i < temArr.length; i++) {
            System.out.println(temArr[i]);
        }
        for (int i = 0; i < temArr.length; i++) {
            if (temArr[i] == 0) {
                x = i / 4;
                y = i % 4;
                date[i / 4][i % 4] = temArr[i];
            } else {
                date[i / 4][i % 4] = temArr[i];
            }
        }
    }

    //添加图片,先加载的图片在上方，后加载的图片在下方
    private void initImage() {
        this.getContentPane().removeAll();
        JLabel stepcount = new JLabel("步数:" + step);
        stepcount.setBounds(50, 30, 100, 30);
        this.getContentPane().add(stepcount);
        if (victory()) {
            JLabel winjLabel = new JLabel(new ImageIcon("image/win.png"));
            winjLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(winjLabel);
        }
        //对图片进行打乱
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = date[i][j];
                JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));
                jLabel.setBounds((105 * i) + 83, (105 * j) + 134, 105, 105);//定位
                jLabel.setBorder(new BevelBorder(0));//添加边框,0叫图片凹下去，选1图片凸起来
                this.getContentPane().add(jLabel);
            }

        }
        //添加背景图片
        ImageIcon bg = new ImageIcon("image\\background.png");
        JLabel background = new JLabel(bg);
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);

        this.getContentPane().repaint();
    }

    //添加界面组件及其定位
    private void initJFrame() {
        //设置界面宽高
        this.setSize(603, 680);
        //设置界面标题
        this.setTitle("拼图游戏单机版v1.0");
        //设置界面置顶(点击其他界面还在最上层)
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式(点×结束程序运行)
        this.setDefaultCloseOperation(3);//3为关一个界面程序停止(WindowConstants.EXIT_ON_CLOSE)2为关闭所有界面才停止
        //取消默认的居中放置，取消了才能实现图片的x y 轴放置
        this.setLayout(null);

        this.addKeyListener(this);

    }

    //添加菜单组件
    private void initJMenuBar() {
        //创建整个菜单对象
        JMenuBar JMenueBar = new JMenuBar();
        //创建菜单上面两个选项(功能，关于作者）
        JMenu functionJMenu = new JMenu("功能");
        JMenu AboutJMenu = new JMenu("关于作者");
        JMenu ChangeImage = new JMenu("更换图片");

        //将更换图片添加到功能里面
        functionJMenu.add(ChangeImage);
        //将其他条目添加到功能中
        functionJMenu.add(replayItem);
        functionJMenu.add(reloginItem);
        functionJMenu.add(closeItem);
        //将大类图片选项添加到更换图片里面
        ChangeImage.add(animals);
        ChangeImage.add(girls);
        ChangeImage.add(sports);
        //添加进入鼠标监听中
        replayItem.addActionListener(this);
        reloginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        animals.addActionListener(this);
        girls.addActionListener(this);
        sports.addActionListener(this);
        //将联系我添加到关于作者中
        AboutJMenu.add(accountItem);
        //将功能与作者添加到菜单中
        JMenueBar.add(functionJMenu);
        JMenueBar.add(AboutJMenu);
        //给整个界面设置菜单
        this.setJMenuBar(JMenueBar);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override//按下不松时调用方法
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == 65) {
            //删除所有图片
            this.getContentPane().removeAll();
            //加载完整图片
            JLabel all = new JLabel(new ImageIcon(path+"/all.jpg"));
            all.setBounds(83, 134, 420, 420);
            this.getContentPane().add(all);

            //添加背景图片
            ImageIcon bg = new ImageIcon("image\\background.png");
            JLabel background = new JLabel(bg);
            background.setBounds(40, 40, 508, 560);
            this.getContentPane().add(background);
            this.getContentPane().repaint();

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (victory()) {
            return;
        }
        int code = e.getKeyCode();
        if (code == 37) {
            if (x == 3) {
                return;
            }
            System.out.println("向左移动");
            //空白方块右边的的方块向左移动
            date[x][y] = date[x + 1][y];
            date[x + 1][y] = 0;
            x++;
            step++;
            initImage();
        } else if (code == 38) {
            if (y == 3) {
                return;
            }
            System.out.println("向上移动");
            //空白方块下面的方块向上移动移动
            date[x][y] = date[x][y + 1];
            date[x][y + 1] = 0;
            y++;
            step++;
            initImage();
        } else if (code == 39) {
            if (x == 0) {
                return;
            }
            System.out.println("向右移动");
            //空白方块左边的方块向右移动
            date[x][y] = date[x - 1][y];
            date[x - 1][y] = 0;
            x--;
            step++;
            initImage();
        } else if (code == 40) {
            if (y == 0) {
                return;
            }
            System.out.println("向下移动");
            //空白方块上面的方块向上移动
            date[x][y] = date[x][y - 1];
            date[x][y - 1] = 0;
            y--;
            step++;
            initImage();
        } else if (code == 65) {
            initImage();
        } else if (code == 87) {
            date = new int[][]{
                    {1, 5, 9, 13},
                    {2, 6, 10, 14},
                    {3, 7, 11, 15},
                    {4, 8, 12, 0}
            };
            initImage();
        }

    }

    public boolean victory() {
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                if (date[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    //菜单栏整理
    @Override
    public void actionPerformed(ActionEvent e) {
        Object oj = e.getSource();
        if (oj == replayItem) {
            System.out.println("重新游戏");
            step = 0;
            initData();
            initImage();
        } else if (oj == reloginItem) {
            System.out.println("重新登录");
            this.setVisible(false);
            new LogingJframe();
        } else if (oj == closeItem) {
            System.out.println("关闭游戏");
            System.exit(0);//关闭虚拟机
        } else if (oj == accountItem) {
            System.out.println("弹出微信");
            //新建一个弹框
            JDialog jdl = new JDialog();
            //建立一个管理图片的容器
            JLabel jlb = new JLabel(new ImageIcon("D:\\Java\\pingtuyouxi\\image\\Wechat.png"));
            //设置容器位置与宽高
            jlb.setBounds(0, 0, 258, 352);
            //将容器添加进弹框
            jdl.getContentPane().add(jlb);
            jdl.setTitle("作者微信，扫码联系");
            //设置弹框的大小
            jdl.setSize(344, 344);
            //让弹框置顶
            jdl.setAlwaysOnTop(true);
            //让弹框居中
            jdl.setLocationRelativeTo(null);
            //弹框不关闭，无法进行其他操作
            jdl.setModal(true);
            //让弹框显示
            jdl.setVisible(true);
        } else if (oj == animals) {
            Random r = new Random();
            int index = r.nextInt(1, 8);
            path = "image\\animal\\animal"+index+"\\";
            step = 0;
            initData();
            initImage();
        } else if (oj == girls) {
            Random r = new Random();
            int index = r.nextInt(1, 13);
            path ="image/girl/girl"+index+"\\";
            step = 0;
            initData();
            initImage();
        } else if (oj == sports) {
            Random r = new Random();
            int index = r.nextInt(1, 10);
            path = "image/sport/sport"+index+"\\";
            step = 0;
            initData();
            initImage();
        }
    }
}
